package de.fhd.pms.ctrl.validations;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import de.fhd.pms.model.User;

/*
 * taken from http://www.mkyong.com/spring-mvc/spring-mvc-password-example/
 * */

public class PasswordValidator implements Validator {

	@Override
	public boolean supports(Class clazz) {
		// just validate the Customer instances
		return User.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {

		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
				"required.password", "Field name is required.");

		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword",
				"required.confirmPassword", "Field name is required.");

		User user = (User) target;

		if (!(user.getPassword().equals(user.getPasswordConfirm()))) {
			errors.rejectValue("password", "notmatch.password");
		}

	}

}
